package com.zzq.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzq.constant.RedisConstant;
import com.zzq.dao.SetmealMapper;
import com.zzq.dao.SetmealTravelGroupMapper;
import com.zzq.entity.PageResult;
import com.zzq.entity.QueryPageBean;
import com.zzq.pojo.Setmeal;
import com.zzq.pojo.SetmealExample;
import com.zzq.pojo.SetmealTravelGroupExample;
import com.zzq.pojo.SetmealTravelGroupKey;
import com.zzq.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

/**
 * 幼儿猿大班
 * 2021/7/23
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealTravelGroupMapper setmealTravelGroupMapper;

    @Autowired
    private JedisPool jedisPool;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        SetmealExample setmealExample = new SetmealExample();
        if(queryPageBean.getQueryString()!=null && !"".equals(queryPageBean.getQueryString())){
            setmealExample.createCriteria().andCodeEqualTo(queryPageBean.getQueryString());
            setmealExample.or().andNameLike("%"+queryPageBean.getQueryString()+"%");
            setmealExample.or().andHelpcodeEqualTo(queryPageBean.getQueryString());
        }
        List<Setmeal> setmeals = setmealMapper.selectByExample(setmealExample);
        PageInfo<Setmeal> setmealPageInfo = new PageInfo<>(setmeals);
        return new PageResult(setmealPageInfo.getTotal(),setmealPageInfo.getList());
    }

    @Override
    public void delete(Integer id) {
        setmealMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Setmeal setmeal, Integer[] travelgroupIds) {
        Jedis jedis = jedisPool.getResource();
        jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        setmealMapper.insert(setmeal);
        jedis.close();
        for (Integer travelgroupId : travelgroupIds) {
            setmealTravelGroupMapper.insert(new SetmealTravelGroupKey(setmeal.getId(),travelgroupId));
        }
    }

    @Override
    public Setmeal findById(Integer id) {
       return setmealMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Integer> findSetmealIdTravelGroupId(Integer id) {
        SetmealTravelGroupExample setmealTravelGroupExample = new SetmealTravelGroupExample();
        setmealTravelGroupExample.createCriteria().andSetmealIdEqualTo(id);
        List<SetmealTravelGroupKey> setmealTravelGroupKeys = setmealTravelGroupMapper.selectByExample(setmealTravelGroupExample);
        ArrayList<Integer> integers = new ArrayList<>();
        for (SetmealTravelGroupKey setmealTravelGroupKey : setmealTravelGroupKeys) {
            integers.add(setmealTravelGroupKey.getTravelgroupId());
        }
        return integers;
    }

    @Override
    public void update(Integer[] travelgroupIds, Setmeal setmeal) {
        setmealMapper.updateByPrimaryKey(setmeal);
        SetmealTravelGroupExample setmealTravelGroupExample = new SetmealTravelGroupExample();
        setmealTravelGroupExample.createCriteria().andSetmealIdEqualTo(setmeal.getId());
        setmealTravelGroupMapper.deleteByExample(setmealTravelGroupExample);
        for (Integer travelgroupId : travelgroupIds) {
            setmealTravelGroupMapper.insert(new SetmealTravelGroupKey(setmeal.getId(),travelgroupId));
        }
    }

    @Override
    public List<Setmeal> findAllSetmeal() {

        List<Setmeal> setmeals = setmealMapper.selectByExample(null);
        return setmeals;
    }

    @Override
    public Setmeal findBySetmealId(Integer id) {
      return setmealMapper.findBySetmealId(id);
    }
}
